package com.xuxu.blog.controller.alipay;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xuxu.blog.dao.PointDao;
import com.xuxu.blog.dao.UserDao;
import com.xuxu.blog.entity.PointEntity;
import com.xuxu.blog.entity.TOrdersEntity;
import com.xuxu.blog.entity.UserEntity;
import com.xuxu.blog.service.TOrdersService;
import com.xuxu.common.utils.PageUtils;
import com.xuxu.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;


/**
 * 
 *
 * @author maxuxu
 * @email maxuxu019@gmail.com
 * @date 2022-08-21 22:22:31
 */
@RestController
@RequestMapping("/blog/torder")
@Api(value = "TOrderPayController",tags = "充值积分")
@Slf4j
public class TOrdersController {
    @Autowired
    private TOrdersService tOrdersService;
    @Resource
    private HttpServletRequest request;
    @Resource
    UserDao userDao;
    @Resource
    PointDao pointDao;
    public static Integer Uid;
    @GetMapping("/buy/{id}")
    @ApiOperation("支付宝请求")
    public R buy(@PathVariable Long id) {
        String userId = request.getHeader("X-ID");
        PointEntity point = pointDao.selectById(id);
        String orderNo = IdUtil.randomUUID();
        String payUrl = "http://192.168.123.223:884/api/blog/alipay/pay?subject=" + point.getPoint() + "&traceNo=" + orderNo + "&totalAmount=" + point.getMoney() + "&totalPoints=" + point.getPoint();
        UserEntity user = userDao.selectOne(Wrappers.<UserEntity>lambdaQuery().eq(UserEntity::getId, Integer.parseInt(userId)));
        TOrdersEntity order = new TOrdersEntity();
        order.setOrderNo(orderNo);
        order.setTotalPrice(point.getPoint());
        order.setPayPrice(point.getMoney());

        order.setUserId(user.getId());
        Uid = user.getId();
        order.setUsername(user.getUsername());
        order.setName(point.getName());
        save(order);
//        orderMapper.insert(order);
        // 新建订单，扣减库存
//        orderMapper.insertOrderTemplate(order);

        String token = request.getHeader("token");
        System.out.println(token);

        return R.ok().put("data",payUrl);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ApiOperation("分页查询订单")
            public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tOrdersService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
            public R info(@PathVariable("id") Integer id){
		TOrdersEntity tOrders = tOrdersService.getById(id);

        return R.ok().put("tOrders", tOrders);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ApiOperation("保存订单")
            public R save(@RequestBody TOrdersEntity tOrders){
		tOrdersService.save(tOrders);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
            public R update(@RequestBody TOrdersEntity tOrders){
		tOrdersService.updateById(tOrders);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
            public R delete(@RequestBody Integer[] ids){
		tOrdersService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
